#import "RNPathComponentCell.h"
#import "NSShadow+MCAdditions.h"
#import "NSBezierPath+MCAdditions.h"

static NSColor *navBarHighlightColor;
static NSColor *navBarStrokeColor;
static NSColor *bracketColor;
static NSShadow *bracketShadow;
static NSShadow *pressedInnerShadow;
static NSGradient *bracketPressedGradient;
static NSBezierPath *bracketPath;
static NSAffineTransform *transformBracketOnePixelRight;
static NSDictionary *titleAttributes;
static CGFloat const bracketWidth = 7.0f;
static CGFloat const bracketShadowWidth = 0.0f;

@implementation RNPathComponentCell

+(void)initialize {
    navBarHighlightColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.5];
    navBarStrokeColor = [NSColor colorWithCalibratedWhite:0.1 alpha:0.5];
    bracketColor = [NSColor colorWithCalibratedWhite:0.1 alpha:0.66];
    bracketPressedGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.5f alpha:1.0f] endingColor:[NSColor colorWithCalibratedWhite:0.67f alpha:1.0f]];
    pressedInnerShadow = [[NSShadow alloc] initWithColor:[NSColor colorWithCalibratedWhite:0.4f alpha:1.0f] offset:NSZeroSize blurRadius:4.0f];
    bracketShadow = [[NSShadow alloc] initWithColor:[NSColor colorWithCalibratedWhite:0.2 alpha:0.75] offset:NSMakeSize(bracketShadowWidth, 0.0) blurRadius:bracketShadowWidth];
    bracketPath = [NSBezierPath bezierPath];
    [bracketPath moveToPoint:NSMakePoint(0, 0)];
    [bracketPath lineToPoint:NSMakePoint(bracketWidth, 10)];
    [bracketPath lineToPoint:NSMakePoint(0, 20)];
    transformBracketOnePixelRight = [NSAffineTransform transform];
    [transformBracketOnePixelRight translateXBy:1.0f yBy:0.0f];
    titleAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:11.0f], NSFontAttributeName, [[NSShadow alloc] initWithColor:[NSColor colorWithCalibratedWhite:0.9f alpha:1.0f] offset:NSMakeSize(0.0f, -1.0f) blurRadius:1.0f], NSShadowAttributeName, nil]; 
}

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    CGFloat maxX = NSMaxX(cellFrame) - 0.5;
    CGFloat minX = NSMinX(cellFrame) + 0.5;
    CGFloat maxY = NSMaxY(cellFrame);
    CGFloat minY = NSMinY(cellFrame);
    CGFloat midY = NSMidY(cellFrame);
    NSRect titleRect = NSInsetRect([self titleRectForBounds:cellFrame], -1, 0);  // resize accounts for subtle difference in font from the stock path component cell
    NSArray *pathCells = [(NSPathControl *)controlView pathComponentCells];
    if ([pathCells objectAtIndex:0] == self) {
        [navBarHighlightColor setStroke];
        [NSBezierPath strokeLineFromPoint:NSMakePoint(minX, minY) toPoint:NSMakePoint(minX, maxY)];
    }
    if ([pathCells lastObject] == self) {
        [navBarStrokeColor setStroke];
        [NSBezierPath strokeLineFromPoint:NSMakePoint(maxX, minY) toPoint:NSMakePoint(maxX, maxY)];
        
        NSBezierPath *pressedPath = [NSBezierPath bezierPath];
        if ([pathCells count] == 1) {
            pressedPath = [NSBezierPath bezierPathWithRect:cellFrame];
        }
        else {
            [pressedPath moveToPoint:NSMakePoint(maxX, minY)];
            [pressedPath lineToPoint:NSMakePoint(minX - bracketWidth - 1, minY)];
            [pressedPath lineToPoint:NSMakePoint(minX - 1, midY)];
            [pressedPath lineToPoint:NSMakePoint(minX - bracketWidth - 1, maxY)];
            [pressedPath lineToPoint:NSMakePoint(maxX, maxY)];
            [pressedPath closePath];
        }
        [bracketPressedGradient drawInBezierPath:pressedPath angle:90];
        [pressedPath fillWithInnerShadow:pressedInnerShadow];
    }
    else {
        NSAffineTransform *translationTransform = [NSAffineTransform transform];
        [translationTransform translateXBy:maxX - bracketWidth yBy:minY];
        NSBezierPath *bracketCopy = [bracketPath copy];
        [bracketCopy transformUsingAffineTransform:translationTransform];
        [bracketColor setStroke];
        [bracketCopy stroke];

        [bracketCopy transformUsingAffineTransform:transformBracketOnePixelRight];
        [navBarHighlightColor setStroke];
        [bracketCopy stroke];
    }
    
    [[self attributedStringValue] drawInRect:titleRect];
}

-(NSAttributedString *)attributedStringValue {
    return [[NSAttributedString alloc] initWithString:[self stringValue] attributes:titleAttributes];
}

@end
